var express = require('express');
var router = express.Router();
var db = require('../models/logDao.js');
var log_server = require('../lib/log_server.js')

/* GET users listing. */
router.get('/', function (req, res, next) {
  var originalUrl = req.originalUrl;
  res.render('resource/node/index', {
    title: '节点列表',
    classes: ['', 'active'],
    originalUrl:originalUrl
  })
});

router.post('/vmhost',function(req,res,next){
  var body = req.body;
  console.log("body: "+JSON.stringify(body))
  //更新每一个websocket连接
  var data = JSON.stringify(body)
  if(log_server.io && log_server.io.sockets){
    log_server.io.sockets.emit("system",{msg:JSON.stringify(data),oneResult:data})
  }
  res.end("success")
})

router.get('/:node_id',function(req,res,next){
  var node_id = req.param('node_id');
  var originalUrl = req.originalUrl;

  //TODO 根据ID查询宿主机
  res.render('resource/node/hostServer/index',{
    title: '宿主机列表',
    classes: ['', 'active'],
    data:node_id,
    originalUrl:originalUrl
  })
})

router.get('/:node_id/:hostServer_id',function(req,res,next){
  var node_id = req.param('node_id');
  var hostServer_id = req.param('hostServer_id');
  var originalUrl = req.originalUrl;
  //TODO 根据ID查询宿主机
  res.render('resource/node/hostServer/vmhost/index',{
    title: '虚拟机列表',
    classes: ['', 'active'],
    data:node_id,
    originalUrl:originalUrl
  })
})

router.get('/:node_id/:hostServer_id/:vm_id',function(req,res,next){
  var node_id = req.param('node_id');
  var hostServer_id = req.param('hostServer_id');
  var vm_id = req.param('vm_id');
  var originalUrl = req.originalUrl;
  //TODO 根据ID查询宿主机
  res.render('resource/node/hostServer/vmhost/chart/index',{
    title: '虚拟机详情',
    classes: ['', 'active'],
    data:node_id,
    originalUrl:originalUrl
  })
})

router.get('/useinit',function(req, res, next){
  var result = db.findByName({type:"status",query:"404"},function(result){
     res.send(result);
  });
})

router.get('/useinit/recent',function(req, res, next){
  var result = db.findRecentByName({type:"status",query:"404"},function(result){
     res.send(result);
  });
})

module.exports = router
